﻿using Microsoft.Xna.Framework.Graphics;

namespace Demo.Framework
{
    public class RenderCapture
    {
        readonly RenderTarget2D _renderTarget;
        readonly GraphicsDevice _graphicsDevice;

        public RenderCapture(GraphicsDevice graphicsDevice)
        {
            this._graphicsDevice = graphicsDevice;
            _renderTarget = new RenderTarget2D(graphicsDevice,
                graphicsDevice.Viewport.Width, graphicsDevice.Viewport.Height,
                false, SurfaceFormat.Color, DepthFormat.Depth24);
        }

        public RenderCapture(GraphicsDevice graphicsDevice, SurfaceFormat format)
        {
            this._graphicsDevice = graphicsDevice;
            _renderTarget = new RenderTarget2D(graphicsDevice,
                graphicsDevice.Viewport.Width, graphicsDevice.Viewport.Height,
                false, format, DepthFormat.Depth24);
        }

        // Begins capturing from the graphics device
        public void Begin()
        {
            _graphicsDevice.SetRenderTarget(_renderTarget);
        }

        // Stop capturing
        public void End()
        {
            _graphicsDevice.SetRenderTarget(null);
        }

        // Returns what was captured
        public Texture2D GetTexture()
        {
            return _renderTarget;
        }
    }
}
